#!/usr/bin/make -f
# debian/rules for MSEGUI-MSEIDE

DEB_DH_BUILDDEB_ARGS := -- -Z bzip2

# Define FPC
ifndef FPC
FPC=/usr/bin/fpc
endif
# Set FPCVER
FPCVER=$(shell ${FPC} -iV)
# Get directories
CURDIR:=$(shell pwd)
BUILD_DIR=$(CURDIR)/debian/build
INSTALL_DIR=$(CURDIR)/debian/tmp
DOC_DIR=${INSTALL_DIR}/usr/share/doc/mseide-msegui
# Set default compilation options
BUILDOPTS=PP=$(FPC)
INSTALLOPTS=INSTALL_PREFIX=$(INSTALL_DIR)/usr
# Define FPCDIR if it was not set
ifndef FPCDIR
export FPCDIR=/usr/share/fpcsrc/${FPCVER}
endif

#export DH_VERBOSE=1

###################
# Clean
#

clean: patch configure clean-patched unpatch
clean-patched:
	@echo "--- Cleaning"
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp install-arch-stamp arrange-arch-stamp configure-stamp
	rm -f build-doc-stamp install-doc-stamp
	rm -f build-indep-stamp install-indep-stamp
	rm -f debian-files-stamp
	rm -f debian/*.files debian/*.docs debian/*.examples debian/*.postinst debian/*.postrm
	rm -f apps/ide/mseide tools/bmp2pas tools/form2pas tools/i18n/msei18n
	find '(' -name '*.a' -or -name '*.o' -or -name '*.so' -or -name '*.ppu' ')' -exec rm -f '{}' ';'
	dh_clean 

patch: patch-stamp

patch-stamp:
	dpatch apply-all -v
	touch patch-stamp

unpatch:
	dpatch deapply-all -v
	rm -rf patch-stamp debian/patched

###################
# Debian files
#

debian-files: debian-files-stamp
debian-files-stamp: patch
	@echo "--- Creating/fixing *.install files"
	#/bin/bash debian/fixdeb debian $(FPCTARGET) $(PPNEW)
	touch debian-files-stamp

###################
# Arch packages
#

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Remove auto-generated make files.
	find -name Makefile.fpc -execdir 'sh' '-c' '${RM} $$(basename {} .fpc)' ';'
	# Regenrate make files using fpcmake.
	#${FPCMAKE} -r -Tall
	touch configure-stamp

build-arch: build-arch-stamp
build-arch-stamp: debian-files-stamp configure-stamp
	@echo "--- Building"
	dh_testdir
	sh $(CURDIR)/debian/build_all.sh
	touch build-arch-stamp

install-arch: install-arch-stamp
install-arch-stamp: build-arch-stamp debian-files-stamp
	@echo "--- Installing"
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	# create all necessary directories
	install -d $(INSTALL_DIR)/usr/bin
	install -d $(INSTALL_DIR)/usr/lib/mseide-msegui/units/common
	install -d $(INSTALL_DIR)/usr/lib/mseide-msegui/apps/ide
	install -d $(INSTALL_DIR)/usr/lib/mseide-msegui/tools/i18n
	# Install the IDE & its accessories
	install -m 755 $(CURDIR)/apps/ide/mseide $(INSTALL_DIR)/usr/lib/mseide-msegui/apps/ide
	cp -Rfpl -t $(INSTALL_DIR)/usr/lib/mseide-msegui/apps/ide apps/ide/syntaxdefs apps/ide/templates
	# Install the tools 
	install -m 755 $(CURDIR)/tools/bmp2pas $(INSTALL_DIR)/usr/lib/mseide-msegui/tools/bmp2pas
	install -m 755 $(CURDIR)/tools/form2pas $(INSTALL_DIR)/usr/lib/mseide-msegui/tools/form2pas
	install -m 755 $(CURDIR)/tools/i18n/msei18n $(INSTALL_DIR)/usr/lib/mseide-msegui/tools/i18n/msei18n
	# Install the library files
	find -name '*.ppu' -or -name '*.[oa]' -or -name '*.so*' | egrep -ve "\/(designutils|apps|tools)\/" | xargs cp -Rfpl -t $(INSTALL_DIR)/usr/lib/mseide-msegui/units/common
	# Install startup scripts & links
	install -m 755 $(CURDIR)/install/mseide $(INSTALL_DIR)/usr/bin/mseide
	install -m 755 $(CURDIR)/install/mseide-icewm $(INSTALL_DIR)/usr/bin/mseide-icewm
	# copy icons and menu entries for the GNOME menu
	install -d $(INSTALL_DIR)/usr/share/pixmaps
	install -d $(INSTALL_DIR)/usr/share/applications
	#convert -geometry 32x32 $(CURDIR)/images/mseide_32x32.png $(INSTALL_DIR)/usr/share/pixmaps/mseide.xpm
	cp $(CURDIR)/images/mseide_32x32.xpm $(INSTALL_DIR)/usr/share/pixmaps/mseide.xpm
	chmod 644 $(INSTALL_DIR)/usr/share/pixmaps/*.xpm
	install -m 644 $(CURDIR)/install/mseide.desktop $(INSTALL_DIR)/usr/share/applications/mseide.desktop
	# copy the contrib area
	install -d $(INSTALL_DIR)/usr/lib/mseide-msegui/contrib
	cp -Rfpl -t $(INSTALL_DIR)/usr/lib/mseide-msegui/contrib $(CURDIR)/contrib/*
	# copy the misc area
	install -d $(INSTALL_DIR)/usr/lib/mseide-msegui/misc
	cp -Rfpl -t $(INSTALL_DIR)/usr/lib/mseide-msegui/misc $(CURDIR)/misc/*
	chmod 755 $(INSTALL_DIR)/usr/lib/mseide-msegui/misc/xft2gs/do_xft.sh
	chmod 755 $(INSTALL_DIR)/usr/lib/mseide-msegui/misc/svn_upd_script/updatefromsvn
	chmod 755 $(INSTALL_DIR)/usr/lib/mseide-msegui/misc/doc_gen/fpc/*.sh
	chmod 755 $(INSTALL_DIR)/usr/lib/mseide-msegui/misc/doc_gen/msegui/*.sh
	# Install man pages
	#$(MAKE) -C install/man installman $(INSTALLOPTS) INSTALL_PREFIX=$(INSTALL_DIR)/usr/share
	touch install-arch-stamp

arrange-arch: arrange-arch-stamp
arrange-arch-stamp: install-arch-stamp debian-files-stamp
	dh_testdir
	dh_testroot
	dh_install -s --sourcedir=debian/tmp --list-missing

	touch arrange-arch-stamp

build-indep: patch
install-indep:
	touch install-indep-stamp

###################
# Documentation
#

build-doc: patch build-doc-stamp
build-doc-stamp: debian-files-stamp
	@echo "--- Building Documentation"
	dh_testdir
	#$(MAKE) -C docs $(CONVERTER) $(BUILDOPTS) FPDOC=$(NEWFPDOC)
	touch build-indep-stamp

install-doc: install-doc-stamp
install-doc-stamp: build-doc-stamp debian-files-stamp
	@echo "--- Installing Documentation"
	dh_testdir
	dh_testroot
	dh_clean -k
	install -d ${DOC_DIR}
	install -m 644 $(CURDIR)/README.TXT ${DOC_DIR}
	install -m 644 $(CURDIR)/VERSION.TXT ${DOC_DIR}
	install -m 644 $(CURDIR)/ZEOS.TXT ${DOC_DIR}
	cp -Rfpl -t ${DOC_DIR} docs/*
	touch install-doc-stamp

###################
# Source
#

install-source: install-source-stamp
install-source-stamp:
	@echo "--- Cleaning the tree and copying the source code"
	dh_testdir
	dh_testroot
	install -d $(INSTALL_DIR)/usr/src/mseide-msegui/
	cp -Rfpl -t $(INSTALL_DIR)/usr/src/mseide-msegui \
	$(CURDIR)/apps \
	$(CURDIR)/lib \
	$(CURDIR)/patch_* \
	$(CURDIR)/tools \
	$(CURDIR)/debian/build_all.sh \
	$(CURDIR)/debian/clean_all.sh \
	$(CURDIR)/README.TXT \
	$(CURDIR)/VERSION.TXT \
	$(CURDIR)/ZEOS.TXT
	find $(INSTALL_DIR)/usr/src/mseide-msegui \
	  \( -name 'COPYING*' -o -name '*.sh' \) -delete
	find $(INSTALL_DIR)/usr/src/mseide-msegui -type f | xargs chmod -x
	touch install-source-stamp

###################
# Generic
#

build: build-arch build-indep
install: install-arch install-indep
binary: binary-arch binary-indep


###################
# Deb building
#

binary-indep: clean-patched build-doc install-doc install-source debian-files
	@echo "--- Building: arch-indep packages"
	dh_testdir
	dh_testroot
	dh_installdocs -i
	dh_installchangelogs -i
	dh_install -i --sourcedir=debian/tmp --list-missing
#	install -m644 -D debian/overrides/omseide-msegui-src \
#	  debian/mseide-msegui-src/usr/share/lintian/overrides/mseide-msegui-src
	dh_compress -i -X.pdf
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i $(DEB_DH_BUILDDEB_ARGS)

binary-arch: build-arch arrange-arch  debian-files
	@echo "--- Building: arch packages"
	dh_testdir
	dh_testroot
	dh_link
	dh_installdocs -a
	dh_installchangelogs -a
	dh_installexamples -a
	dh_installmenu
	fpc-depends
	#dh_installman -s
	dh_strip -s
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -s
	dh_shlibdeps -s
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s $(DEB_DH_BUILDDEB_ARGS)

.PHONY: build clean binary binary-arch \
	binary-indep debian-files build-arch \
	install install-indep install-arch \
	configure

get-orig-source:
	uscan --force-download --rename
